package comWorld.msg.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gjs.tools.utils.Utils;

import comWorld.msg.Message;
import comWorld.msg.MessageManage;
import comWorld.user.UserInfo;

/**
 * Servlet implementation class InBoxPageInit
 */
@WebServlet("/admin/inbox")
public class InBoxPageInit extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public InBoxPageInit() {
        super();
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		UserInfo userInfo = (UserInfo) request.getSession().getAttribute("visitorInfo");
		String box = request.getParameter("box");
		if (box == null || box.length() == 0) {
			box = "all";
		}
		request.setAttribute("box", box);
		
		List<Message> msgs = MessageManage.getMsgs(userInfo.getId(), "box", box);
		
		int[] be = Utils.paging(request, 10, msgs.size());
		
		request.setAttribute("msgs", msgs.subList(be[0], be[1]));
		request.getRequestDispatcher("/msg/inbox.jsp").forward(request, response);
		
	}

}
